package com.smartphonedev.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;

import com.smartphonedev.files.FileUtils;


public class Tab3Activity extends Activity {

	final Context context = this;
	WebView webview;

	@Override
	public void onCreate(Bundle savedState) {
		super.onCreate(savedState);
		setContentView(R.layout.tab3);

		webview = (WebView) findViewById(R.id.webview_history);
		setViewTab3();
	}

	public void setButtonRefreshHistoty(View v) {
		setViewTab3();
		refreshDone();
	}

	public void setViewTab3() {

		String mimeType = "text/html";
		String encoding = "UTF-8";
		String code = FileUtils.getTextFromFile(context.getString(R.string.filename), context);
		String code2 = "<?xml version='1.0' encoding='UTF-8'?><div style='text-align:center'><div style='background:#dddddd;padding:5px'><div style='background:white;padding:10px;border:1px solid #999999'><font color='#3366ff'><marquee behavior='left' loop='infinite'>" + context.getString(R.string.historydes) + "</marquee></div></div></div>"; 
		if (code.equals("")) {

			webview.loadData(code2, mimeType, encoding);

		} else {
			webview.loadData(code, mimeType, encoding);
		}

	}

	public void setButtonDeleteHistoty(View v) {
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(context.getString(R.string.confirm));
		adb.setIcon(R.drawable.info2);
		adb.setMessage(context.getString(R.string.confirmdelete));
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				FileUtils.deleteFile(context.getString(R.string.filename), context);
				setViewTab3();
				deleteDone();
			}
		});
		adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		adb.show();
	}

	public void setButtonInfoHistoty(View v) {
		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(context.getString(R.string.infob));
		adb.setIcon(R.drawable.info2);
		adb.setMessage(context.getString(R.string.historyinfo));
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		adb.show();
	}

	public void refreshDone() {

		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(context.getString(R.string.confirm));
		adb.setIcon(R.drawable.info2);
		adb.setMessage(context.getString(R.string.refreshdone));
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				setViewTab3();
			}
		});

		adb.show();

	}

	public void deleteDone() {

		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		adb.setTitle(context.getString(R.string.confirm));
		adb.setIcon(R.drawable.info2);
		adb.setMessage(context.getString(R.string.deletedone));
		adb.setCancelable(false);
		adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				setViewTab3();
			}
		});

		adb.show();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_exit2:
			Tab3Activity.this.finish();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		super.onKeyDown(keyCode, event);
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			AlertDialog.Builder adb = new AlertDialog.Builder(this);
			adb.setTitle(R.string.confirm);
			adb.setIcon(R.drawable.exit);
			adb.setMessage(R.string.confirmexit);
			adb.setCancelable(false);
			adb.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Tab3Activity.this.finish();
				}
			});
			adb.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			adb.show();

			return true;
		}
		return false;
	}



}
